<template>
  <div class="bigbox">
    <div class="topbox" :style="{ height: isheight + 'px' }">
      <el-form
        :model="form"
        ref="form"
        :inline="true"
        label-position="right"
        label-width="70px"
        onsubmit="return false"
      >
        <el-col :span="6">
          <el-form-item label="用电企业">
            <el-select
              v-model="bizOrgName"
              style="width: 100%"
              size="small"
              ref="shou1"
            >
              <el-option
                class="option"
                hidden
                :label="form.bizOrgName"
                :value="bizOrgName"
                suffix-icon="el-icon-date"
              >
              </el-option>
              <el-input
                style="width: 90%; margin-left: 5%"
                placeholder="输入关键字进行过滤"
                v-model="filterText"
              />
              <el-tree
                :props="props"
                :load="loadNode"
                lazy
                :filter-node-method="filterNode"
                @node-click="handleNodeClick"
                ref="tree"
              >
              </el-tree>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6"> </el-col>
        <el-col :span="6"> </el-col>
        <el-col :span="6">
          <el-form-item>
            <div style="text-align: right !important">
              <button @click="chongzhi" class="button">重置</button>
              <button @click="submit" class="button chaxun">查询</button>
            </div>
          </el-form-item>
        </el-col>
      </el-form>
    </div>
    <div class="bodybox">
      <div class="titlebox">
        <div class="text">电价配置信息</div>
        <div
          class="right"
          :style="{ alignItems: 'center', lineHeight: '32.5px' }"
        >
          <button
            type="primary"
            size="small"
            class="bewdao daochu"
            @click="add"
          >
            新增
          </button>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <span
              @click="shuaxin"
              :style="{ cursor: 'pointer', fontSize: '20px' }"
              ><i class="el-icon-refresh-right"></i
            ></span>
          </el-tooltip>
          <el-dropdown trigger="click" @command="command">
            <span class="el-dropdown-link">
              <el-tooltip
                class="item"
                effect="dark"
                content="密度"
                placement="top"
              >
                <span :style="{ cursor: 'pointer', fontSize: '20px' }"
                  ><i class="el-icon-d-caret"></i
                ></span>
              </el-tooltip>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item class="dropdown-list" command="默认"
                >默认</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="中等"
                >中等</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="紧凑"
                >紧凑</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <div>
        <template>
          <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            border
            id="exportTabyong"
            ref="table"
            :size="iissize"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center',
            }"
          >
            <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
            <el-table-column
              align="center"
              type="index"
              label="序号"
              width="50"
            >
            </el-table-column>
            <el-table-column
              prop="companyName"
              label="用电企业"
              width="280"
            >
            </el-table-column>
            <el-table-column
              prop="measurePointName"
              label="计量回路"
              width="150"
            >
            </el-table-column>
            <el-table-column
              prop="eleProperty"
              label="用电类型"
              width="100"
            >
            </el-table-column>
            <el-table-column
              prop="degreePriceName"
              label="电度电价方案"
            >
            </el-table-column>
            <el-table-column
              prop="basicArPriceName"
              label="按容基本电价方案"
            >
            </el-table-column>
            <el-table-column
              prop="basicAxPriceName"
              label="按实际需量基本电价方案"
            >
            </el-table-column>
            <el-table-column
              prop="adjustPriceName"
              label="力调电费方案"
            >
            </el-table-column>
            <el-table-column
              align="center"
              width="200"
              fixed="right"
              label="操作"
            >
              <template slot-scope="scope">
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="编辑"
                  placement="top"
                >
                  <i
                    class="operate el-icon-edit"
                    @click="handleEdit(scope.$index, scope.row)"
                  ></i
                ></el-tooltip>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="删除"
                  placement="top"
                >
                  <i
                    :style="{ marginLeft: '20px', color: 'red' }"
                    @click="handleDelete(scope.$index, scope.row)"
                    class="operate el-icon-delete"
                  ></i
                ></el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <div class="fenye">
          <el-pagination
            background
            layout="prev, pager, next"
            :current-page.sync="pageobj._pageNo"
            @current-change="getdatalist(form.orgNo)"
            :total="totalCount"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="dialog">
      <el-dialog
        title="电费方案配置信息"
        :visible.sync="centerDialogVisible"
        label-position="left"
        :close-on-click-modal="false"
        center
        :before-close="beforeClose"
      >
        <div class="dialog-content">
          <el-form
            ref="dialogForm"
            label-position="left"
            :rules="rules"
            :model="dialogForm"
            label-width="180px"
          >
            <el-row>
              <el-col :span="13">
                <el-form-item label="用电企业">
                  <el-select
                    :disabled="isEdit"
                    v-model="bizOrgName2"
                    style="width: 113%"
                    size="small"
                    ref="shou2"
                  >
                    <el-option
                      class="option"
                      hidden
                      :label="dialogForm.companyName"
                      value="value"
                      suffix-icon="el-icon-date"
                    >
                    </el-option>
                    <el-input
                      style="width: 90%; margin-left: 5%"
                      placeholder="输入关键字进行过滤"
                      v-model="filterText1"
                    />
                    <el-tree
                      :props="props"
                      :load="loadNode"
                      lazy
                      :filter-node-method="filterNode"
                      @node-click="handleNodeClick2"
                      ref="tree"
                    >
                    </el-tree>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="13">
                <el-form-item label="计量回路" prop="measurePointId">
                  <el-select
                    :style="{ width: '260px' }"
                    v-model="dialogForm.measurePointId"
                  >
                    <el-option
                      v-for="item in JLHL"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="13">
                <el-form-item label="用电类型" prop="eleProperty">
                  <el-select
                    :style="{ width: '260px' }"
                    v-model="dialogForm.eleProperty"
                  >
                    <el-option
                      v-for="item in eleProperty"
                      :key="item.label"
                      :label="item.label"
                      :value="item.label"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="13">
                <el-form-item label="电度电价方案" prop="degreePriceId">
                  <el-select
                    :style="{ width: '260px' }"
                    v-model="dialogForm.degreePriceId"
                  >
                    <el-option
                      v-for="item in DDDJ"
                      :key="item.id"
                      :label="item.elePriceTemplateName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="13">
                <el-form-item label="按容基本电价方案" prop="basicArPriceId">
                  <el-select
                    :style="{ width: '260px' }"
                    v-model="dialogForm.basicArPriceId"
                  >
                    <el-option
                      v-for="item in ARJB"
                      :key="item.label"
                      :label="item.elePriceTemplateName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="13">
                <el-form-item
                  label="按实际需量基本电价方案"
                  prop="basicAxPriceId"
                >
                  <el-select
                    :style="{ width: '260px' }"
                    v-model="dialogForm.basicAxPriceId"
                  >
                    <el-option
                      v-for="item in AXJB"
                      :key="item.label"
                      :label="item.elePriceTemplateName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="13">
                <el-form-item label="力调电价方案" prop="adjustPriceId">
                  <el-select
                    :style="{ width: '260px' }"
                    v-model="dialogForm.adjustPriceId"
                  >
                    <el-option
                      v-for="item in LTDJ"
                      :key="item.label"
                      :label="item.elePriceTemplateName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button class="dialogBtn" @click="resetForm">重置</el-button>
          <el-button class="dialogBtn" type="primary" @click="onSubmit"
            >保存</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// import { saveAs } from 'file-saver'
import { gettreelist, gettreexiaji } from '../../api/userscreen'
import {
  delConfigureInfoApi,
  getConfigureInfoApi,
  getConfigureListApi,
  getMeasurePointApi,
  saveConfigureInfoApi
} from '../../api/configure'
import { getloginuserapi, getyonghuxialaapi } from '@/api/cycle'
export default {
  name: 'ConfigureIndex',

  data () {
    return {
      DDDJ: [],
      ARJB: [],
      AXJB: [],
      LTDJ: [],
      JLHL: [],
      rules: {
        measurePointId: [
          { required: true, message: '请选择计量回路', trigger: 'change' }
        ],
        eleProperty: [
          { required: true, message: '请选择用电类型', trigger: 'change' }
        ],
        degreePriceId: [
          { required: true, message: '请选择电度电价方案', trigger: 'change' }
        ],
        adjustPriceId: [
          { required: true, message: '请选择力调电价方案', trigger: 'change' }
        ]
      },
      bizOrgName: '',
      bizOrgName2: '',
      isEdit: false,
      centerDialogVisible: false,
      form: { alarmLevel: '0' },
      dialogForm: {},
      eleProperty: [
        {
          label: '工业'
        },
        {
          label: '商业'
        }
      ],
      filterText: '',
      filterText1: '',
      isshow: false,
      isheight: 72,
      date: [],
      iissize: 'medium',
      props: {
        label: 'bizOrgName'
      },
      loading: false,
      tableData: [],
      orgType: '',
      pageobj: {
        _pageNo: 1,
        _pageSize: 10
      },
      totalCount: 0
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    },
    filterText1 (val) {
      this.$refs.tree.filter(val)
    }
  },
  created () {
    this.$watch('dialogForm.degreePriceId', () => {
      this.dialogForm.degreePriceName = this.DDDJ.find(
        (i) => i.id === this.dialogForm.degreePriceId
      )?.elePriceTemplateName
    })
    this.$watch('dialogForm.basicArPriceId', () => {
      this.dialogForm.basicArPriceName = this.ARJB.find(
        (i) => i.id === this.dialogForm.basicArPriceId
      )?.elePriceTemplateName
    })
    this.$watch('dialogForm.basicAxPriceId', () => {
      this.dialogForm.basicAxPriceName = this.AXJB.find(
        (i) => i.id === this.dialogForm.basicAxPriceId
      )?.elePriceTemplateName
    })
    this.$watch('dialogForm.adjustPriceId', () => {
      this.dialogForm.adjustPriceName = this.LTDJ.find(
        (i) => i.id === this.dialogForm.adjustPriceId
      )?.elePriceTemplateName
    })
    this.$watch('dialogForm.measurePointId', () => {
      this.dialogForm.measurePointName = this.JLHL.find(
        (i) => i.id === this.dialogForm.measurePointId
      )?.name
    })
  },
  async mounted () {
    await this.getchushihua()
    this.getdatalist()
  },

  methods: {
    async getchushihua () {
      const re = await getloginuserapi()
      if (re.data.entity.orgType !== '04' && re.data.entity.orgType !== '40') {
        const res = await gettreelist()
        this.bizOrgName = res.data.list[0].bizOrgName
        this.bizOrgName2 = res.data.list[0].bizOrgName
        this.form.orgNo = res.data.list[0].bizOrgNo
        this.form.orgType = res.data.list[0].bizOrgType
      } else {
        const res = await getyonghuxialaapi()
        this.bizOrgName = res.data.list[0].bizOrgName
        this.bizOrgName2 = res.data.list[0].bizOrgName
        this.form.orgNo = res.data.list[0].bizOrgNo
        this.form.orgType = res.data.list[0].bizOrgType
      }
    },
    async getdatalist (now) {
      this.loading = true
      const res = await getConfigureInfoApi({
        _pageNo: this.pageobj._pageNo - 1,
        _pageSize: this.pageobj._pageSize,
        orgNo: now || this.form.orgNo
      })
      console.log(this.form.orgNo)
      console.log(res)
      this.totalCount = res.data.totalCount
      this.tableData = res?.data.list
      this.loading = false
    },
    async getSelectiondata (orgNo) {
      // 电度电价
      const res1 = await getConfigureListApi({
        elePriceTemplateType: '01',
        orgNo
      })
      this.DDDJ = res1.data?.list || []
      // 按容基本电价
      const res2 = await getConfigureListApi({
        basicDeclareWay: '01',
        elePriceTemplateType: '02',
        orgNo
      })
      this.ARJB = res2.data?.list || []
      console.log('ARJB', this.ARJB)

      // // // 按需基本电价elePriceTemplateName elePriceTemplateNo
      const res3 = await getConfigureListApi({
        basicDeclareWay: '03',
        elePriceTemplateType: '02',
        orgNo
      })
      this.AXJB = res3.data?.list || []
      console.log('AXJB', this.AXJB)
      // // // 力调电价
      const res4 = await getConfigureListApi({
        elePriceTemplateType: '03',
        orgNo
      })
      this.LTDJ = res4.data?.list || []
      // 计量回路
      const res = await getMeasurePointApi({ orgNo })
      this.JLHL = res.data?.list || []
      console.log(res)
    },
    // 刷新列表
    shuaxin () {
      this.getdatalist()
      this.$message.success('列表刷新成功')
    },
    submit () {
      this.getdatalist()
    },
    command (val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },
    // 树组件懒加载
    async loadNode (node, resolve) {
      const re = await getloginuserapi()
      if (re.data.entity.orgType !== '04' && re.data.entity.orgType !== '40') {
        const res = await gettreelist()
        if (node.level === 0) {
          // 初始的级数（最顶层）
          return resolve(res?.data.list) // 最顶层数据渲染为region
        }
        const ress = await gettreexiaji({ orgNo: node.data.bizOrgNo })
        if (node.level === 1) {
          return resolve(ress?.data.list)
        } else if (node.level === 2) {
          return resolve([])
        }
      } else {
        if (node.level === 0) {
          const res = await getyonghuxialaapi()
          console.log(res)
          return resolve(res?.data.list)
        } else {
          return resolve([])
        }
      }
    },
    // 树节点过滤
    filterNode (value, data) {
      if (!value) return true
      return data.bizOrgName.indexOf(value) !== -1
    },

    handleNodeClick (val) {
      this.bizOrgName = val.bizOrgName
      this.form.orgNo = val.bizOrgNo
      this.form.orgType = val.filterNode
      console.log(val)
      if (this.orgType !== '04' || this.orgType !== '40') {
        this.$refs.shou1.blur()
      }
    },
    handleNodeClick2 (val) {
      this.bizOrgName2 = val.bizOrgName
      this.dialogForm.companyName = val.bizOrgName
      this.dialogForm.companyNo = val.bizOrgNo
      console.log(val)
      if (this.orgType !== '04' || this.orgType !== '40') {
        this.$refs.shou2.blur()
      }
      this.getSelectiondata(this.dialogForm.companyNo)
      console.log(this.dialogForm)
    },
    async chongzhi () {
      await this.getchushihua()
      this.date = []
      this.getdatalist()
      this.form.alarmLevel = '0'
    },
    async handleEdit (index, row) {
      console.log(index)
      console.log(row)
      this.isEdit = true
      this.dialogForm = { ...row }
      this.bizOrgName2 = row.companyName
      await this.getSelectiondata(this.dialogForm.companyNo)
      this.centerDialogVisible = true
    },
    add () {
      this.isEdit = false
      this.bizOrgName2 = this.bizOrgName
      this.dialogForm.companyName = this.bizOrgName
      this.dialogForm.companyNo = this.form.orgNo
      this.getSelectiondata(this.form.orgNo)
      this.centerDialogVisible = true
    },
    async handleDelete (index, row) {
      console.log(index)
      console.log(row)
      this.$confirm('此操作将永久删除该配置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            await delConfigureInfoApi(row.id)
            this.$message.success('删除成功')
          } catch (err) {
            this.$message.success('删除失败')
          }
          this.getdatalist(this.dialogForm.companyNo)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑保存
    async onSubmit () {
      await this.$refs.dialogForm.validate()
      console.log(this.dialogForm)
      const res = await saveConfigureInfoApi(this.dialogForm)
      console.log(res)
      this.centerDialogVisible = false
      this.getdatalist(this.dialogForm.companyNo)
      this.$message.success(res.data.content)
    },
    // 重置弹窗里的表单
    resetForm () {
      this.$refs.dialogForm.resetFields()
      this.dialogForm = {}
    },
    beforeClose () {
      this.centerDialogVisible = false
      this.dialogForm = {}
    }
  }
}
</script>

<style lang="scss" scoped>
.option {
  height: auto;
}
.bigbox {
  background-color: #f1f1f1;
  padding: 10px;
  .topbox {
    background-color: #fff;
    padding: 20px 20px 0 0px;
    box-sizing: border-box;
    height: 80px !important;

    // transition-property: all;
    // transition-duration: 0.2s;
    ::v-deep .el-form-item {
      width: 100%;
      margin-left: -10px;
    }
    .button {
      width: 63px;
      height: 32px;
      margin-right: 5px;
      background-color: #fff;
      border: 1px solid #ccc;
      border-radius: 3px;
    }
    .chaxun {
      background-color: #1890ff;
      color: #fff;
    }
  }
  .bodybox {
    padding-top: 20px;
    .titlebox {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      .text {
        font-size: 16px;
      }
      .right {
        align-items: center;
        i {
          margin-left: 10px;
        }
        .bewdao {
          width: 63px;
          height: 32px;
        }
        .daochu {
          background-color: #1890ff;
          color: #fff;
          border: none;
          border-radius: 3px;
        }
      }
    }
    .fenye {
      margin-top: 15px;
      text-align: right;
    }
  }
}

::v-deep .el-form-item__content {
  width: calc(100% - 100px);
}
.bodybox {
  background-color: #fff;
  margin-top: 10px;
  padding: 20px 20px 20px 20px;
}
::v-deep .el-col-6 {
  height: 42px;
  text-align: right;
}
::v-deep .el-input__inner {
  height: 30px;
}
::v-deep.el-form--inline {
  height: 100% !important;
  box-sizing: border-box;
}
::v-deep .el-input__icon {
  line-height: 20px;
}

.dropdown-list {
  text-align: center;
  width: 50px;
}
.operate {
  cursor: pointer;
  color: #1890ff;
  font-size: 14px;
}
.dialogBtn {
  width: 80px;
  height: 40px;
  border-radius: 4px;
  font-size: 14px;
  text-align: center;
  align-items: center;
}
::v-deep .dialog {
  .el-dialog {
    width: 500px;
    height: 550px;
    padding-left: 10px;
    .el-dialog__header {
      padding: 10px 0;
      font-size: 14px;
      text-align: left !important;
    }
    .el-dialog__body {
      width: 660px;
      line-height: 10px;
      padding-bottom: 15px;
    }
    .el-dialog__footer {
      height: 60px;
    }
  }
  .el-row {
    height: 55px;
    .el-col {
      height: 55px;
    }
  }
}
button {
  cursor: pointer;
}
</style>
